package com.eascs.admin.item.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import com.eascs.admin.common.controller.BaseViewController;
import com.eascs.admin.item.entity.O2oPubDatadictionaryEntity;
import com.eascs.admin.item.service.O2oPubDdItemService;
import com.eascs.admin.item.vo.O2oPubDdItemForm;
import com.eascs.admin.item.vo.O2oPubDdItemVo;
import com.eascs.app.dao.PageResult;
import com.eascs.common.util.UrlUtil;
import com.eascs.site.page.PaginationInfo;
import com.eascs.site.view.ViewContext;
import com.eascs.site.view.button.Button;
import com.eascs.site.view.button.ButtonHelper;
import com.eascs.site.view.button.ShowButtonFilter;

@Controller
@RequestMapping("/item")
public class O2oPubDdItemController extends BaseViewController {

	@Autowired
	O2oPubDdItemService ddItemService;

	@SuppressWarnings("unused")
	@RequestMapping("/index")
	public String index(O2oPubDdItemVo vo) {
		vo.setPageSize(10);
		String url = "/item/index";
		if (null != vo) {
			String itemCode = vo.getItemCode() == null ? "" : vo.getItemCode();
			String itemName = vo.getItemName() == null ? "" : vo.getItemName();
			url = UrlUtil.appendUrl(url, "itemCode", itemCode);
			url = UrlUtil.appendUrl(url, "itemName", itemName);
			url = url + "&pageIndex=";
		} else {
			url = url + "&pageIndex=";
		}
		Button detail = ButtonHelper.createOpenButton("管理常量", "/item/detail?ddNo=");
		Button edit = ButtonHelper.createAjaxButton("修改", "/ritem/editDdHtml?ddNo=");
//		Button add = ButtonHelper.createAjaxButton("添加常量", "/ritem/addHtml?ddNo=");
		Button start = ButtonHelper.createAjaxButton("启用", "/ritem/start_up?type=" + O2oPubDdItemForm.DD_START + "&ddNo=");
		start.setShowButtonFilter(new ShowButtonFilter() {

			@Override
			public boolean filter(Object param) {
				Object[] params = (Object[]) param;
				if (params[4].equals("锁定")) {
					return true;
				} else
					return false;
			}
		});
		Button stop = ButtonHelper.createAjaxButton("禁用", "/ritem/start_up?type=" + O2oPubDdItemForm.DD_STOP + "&ddNo=");
		stop.setShowButtonFilter(new ShowButtonFilter() {

			@Override
			public boolean filter(Object param) {
				Object[] params = (Object[]) param;
				if (params[4].equals("未锁定")) {
					return true;
				} else
					return false;
			}
		});
		Button buttons[] = new Button[] { start, stop, edit, detail };
		PageResult<Object[]> pageResult = ddItemService.findPubDatadictionaryByPage(vo);
		String titles[] = { "字典编码", "字典名称", "描述", "状态" };
		PaginationInfo paginationInfo = new PaginationInfo(pageResult, titles, url);
		paginationInfo.setBuildPagingNavigation(true);
		paginationInfo.setFirstRenderColumnIndex(1);
		paginationInfo.setMoreButtons(buttons);
		ViewContext.put("vo", vo);
		ViewContext.put("paginationInfo", paginationInfo);
		return "item/index";
	}

	@RequestMapping("/detail")
	public String detail(O2oPubDdItemVo vo) {
		vo.setPageSize(Integer.MAX_VALUE);
		PageResult<Object[]> pageResult = ddItemService.findPubDdItemByPage(vo);
		String titles[] = { "常量编码", "常量名称", "排序", "描述", "状态" };
		Button edit = ButtonHelper.createAjaxButton("修改", "/ritem/editHtml?ddNo=");
		Button start = ButtonHelper.createAjaxButton("启用", "/ritem/start_up?type=" + O2oPubDdItemForm.DDO_START + "&ddNo=");
		start.setShowButtonFilter(new ShowButtonFilter() {

			@Override
			public boolean filter(Object param) {
				Object[] params = (Object[]) param;
				if (params[5].equals("锁定")) {
					return true;
				} else
					return false;
			}
		});
		Button stop = ButtonHelper.createAjaxButton("禁用", "/ritem/start_up?type=" + O2oPubDdItemForm.DDO_STOP + "&ddNo=");
		stop.setShowButtonFilter(new ShowButtonFilter() {

			@Override
			public boolean filter(Object param) {
				Object[] params = (Object[]) param;
				if (params[5].equals("未锁定")) {
					return true;
				} else
					return false;
			}
		});
		Button buttons[] = new Button[] { start, stop, edit };
		PaginationInfo paginationInfo = new PaginationInfo(pageResult, titles);
		paginationInfo.setBuildPagingNavigation(true);
		paginationInfo.setFirstRenderColumnIndex(1);
		paginationInfo.setMoreButtons(buttons);
		O2oPubDatadictionaryEntity datadictionaryEntity = ddItemService.findPubDatadictionaryById(vo.getDdNo());
		ViewContext.put("paginationInfo", paginationInfo);
		ViewContext.put("vo", datadictionaryEntity);
		return "item/detail";
	}
}
